from flask_login import UserMixin
from sqlalchemy import Column, String, Integer, Boolean
from werkzeug.security import generate_password_hash, check_password_hash

from app.model.base import BaseModel


class User(BaseModel, UserMixin):
    email = Column(String(30), nullable=False, unique=True)
    _password = Column('password', String(400), nullable=False)
    realname = Column(String(10), nullable=False)
    address = Column(String(200), nullable=False)
    tel = Column(String(11), nullable=False)
    admin = Column(Boolean, default=False)

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, raw):
        self._password = generate_password_hash(raw)

    def check_password(self, raw):
        return check_password_hash(self._password, raw)
